package com.craftplay.dao;

import com.craftplay.model.User;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class UserSqlProvider {

    public String insertSelective(User record) {
        BEGIN();
        INSERT_INTO("user");

        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=INTEGER}");
        }

        if (record.getOpenId() != null) {
            VALUES("open_id", "#{openId,jdbcType=VARCHAR}");
        }

        if (record.getNickname() != null) {
            VALUES("nickname", "#{nickname,jdbcType=VARCHAR}");
        }

        if (record.getSex() != null) {
            VALUES("sex", "#{sex,jdbcType=INTEGER}");
        }

        if (record.getProvince() != null) {
            VALUES("province", "#{province,jdbcType=VARCHAR}");
        }

        if (record.getCity() != null) {
            VALUES("city", "#{city,jdbcType=VARCHAR}");
        }

        if (record.getCountry() != null) {
            VALUES("country", "#{country,jdbcType=VARCHAR}");
        }

        if (record.getHeadUrl() != null) {
            VALUES("head_url", "#{headUrl,jdbcType=VARCHAR}");
        }

        if (record.getUnionid() != null) {
            VALUES("unionid", "#{unionid,jdbcType=VARCHAR}");
        }

        if (record.getFeature() != null) {
            VALUES("feature", "#{feature,jdbcType=VARCHAR}");
        }

        if (record.getIntro() != null) {
            VALUES("intro", "#{intro,jdbcType=VARCHAR}");
        }

        if (record.getIntroImgUrl() != null) {
            VALUES("intro_img_url", "#{introImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getPlace() != null) {
            VALUES("place", "#{place,jdbcType=VARCHAR}");
        }

        if (record.getPlaceImgUrl() != null) {
            VALUES("place_img_url", "#{placeImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getTel() != null) {
            VALUES("tel", "#{tel,jdbcType=VARCHAR}");
        }
        if (record.getUserType() != null) {
            VALUES("user_type", "#{userType,jdbcType=INTEGER}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(User record) {
        BEGIN();
        UPDATE("user");

        if (record.getNickname() != null) {
            SET("nickname = #{nickname,jdbcType=VARCHAR}");
        }

        if (record.getSex() != null) {
            SET("sex = #{sex,jdbcType=INTEGER}");
        }

        if (record.getProvince() != null) {
            SET("province = #{province,jdbcType=VARCHAR}");
        }

        if (record.getCity() != null) {
            SET("city = #{city,jdbcType=VARCHAR}");
        }

        if (record.getCountry() != null) {
            SET("country = #{country,jdbcType=VARCHAR}");
        }

        if (record.getHeadUrl() != null) {
            SET("head_url = #{headUrl,jdbcType=VARCHAR}");
        }

        if (record.getUnionid() != null) {
            SET("unionid = #{unionid,jdbcType=VARCHAR}");
        }

        if (record.getFeature() != null) {
            SET("feature = #{feature,jdbcType=VARCHAR}");
        }

        if (record.getIntro() != null) {
            SET("intro = #{intro,jdbcType=VARCHAR}");
        }

        if (record.getIntroImgUrl() != null) {
            SET("intro_img_url = #{introImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getPlace() != null) {
            SET("place = #{place,jdbcType=VARCHAR}");
        }

        if (record.getPlaceImgUrl() != null) {
            SET("place_img_url = #{placeImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getTel() != null) {
            SET("tel = #{tel,jdbcType=VARCHAR}");
        }
        if (record.getUserType() != null) {
            SET("user_type = #{userType,jdbcType=INTEGER}");
        }

        WHERE("user_id = #{userId,jdbcType=INTEGER}");
        WHERE("open_id = #{openId,jdbcType=VARCHAR}");

        return SQL();
    }

    public String selectAllUserByColumn(User record, int page, int limit) {
        BEGIN();
        SELECT("*");
        FROM("user");
        if (record.getNickname() != null) {
            WHERE("nickname = #{nickname,jdbcType=VARCHAR}");
        }

        if (record.getSex() != null) {
            WHERE("sex = #{sex,jdbcType=INTEGER}");
        }

        if (record.getProvince() != null) {
            WHERE("province = #{province,jdbcType=VARCHAR}");
        }

        if (record.getCity() != null) {
            WHERE("city = #{city,jdbcType=VARCHAR}");
        }

        if (record.getCountry() != null) {
            WHERE("country = #{country,jdbcType=VARCHAR}");
        }

        if (record.getHeadUrl() != null) {
            WHERE("head_url = #{headUrl,jdbcType=VARCHAR}");
        }

        if (record.getUnionid() != null) {
            WHERE("unionid = #{unionid,jdbcType=VARCHAR}");
        }

        if (record.getFeature() != null) {
            WHERE("feature = #{feature,jdbcType=VARCHAR}");
        }

        if (record.getIntro() != null) {
            WHERE("intro = #{intro,jdbcType=VARCHAR}");
        }

        if (record.getIntroImgUrl() != null) {
            WHERE("intro_img_url = #{introImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getPlace() != null) {
            WHERE("place = #{place,jdbcType=VARCHAR}");
        }

        if (record.getPlaceImgUrl() != null) {
            WHERE("place_img_url = #{placeImgUrl,jdbcType=VARCHAR}");
        }

        if (record.getTel() != null) {
            WHERE("tel = #{tel,jdbcType=VARCHAR}");
        }

        return SQL();
    }

}